<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/OperatorBase.php' );

class profile extends ApplicationBase {

    // constructor
    public function __construct() {
        parent::__construct();
        // load model
        $this->load->model('m_operator_mini');
        $this->load->model('m_kategoribarang');
        // load library
        $this->load->library('tnotification');
        $this->load->library('pagination');
        // load helpers
    }

    // welcome administrator
    public function index() {
        // set template content
        $this->_set_page_rule("R");

        // set template content
        $this->smarty->assign("template_content", "pengaturan/profile/edit_profile.html");

        //data
        $result = $this->m_operator_mini->get_operator_by_id($this->com_user['user_id']);
        if (!empty($result)) {
            $result['user_pass'] = $this->encrypt->decode($result['user_pass'], $result['user_key']);
            $this->smarty->assign("result", $result);
        }

        $filepath = 'resource/doc/images/users/' . $result['user_img'];
        if (!is_file($filepath)) {
            $filepath = 'resource/doc/images/users/default.png';
        }
        $user_img = base_url() . $filepath;
        // assign
        $this->smarty->assign("user_img", $user_img);

        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function edit_process() {
        //get data
        $this->tnotification->set_rules('user_name', 'Username', 'trim|required');
        $this->tnotification->set_rules('user_pass', 'Password', 'trim|required');
        $this->tnotification->set_rules('petugas_name', 'Nama', 'trim|required');
        $username = trim($this->input->post('user_name'));

        // cek username         
        if ($this->m_operator_mini->is_exist_username($username) != NULL) {
            $this->tnotification->sent_notification("error", 'Username is not available');
        }

        if ($this->tnotification->run() !== FALSE) {
            $password_key = crc32($this->input->post('user_pass'));
            $password = $this->encrypt->encode($this->input->post('user_pass'), $password_key);

            $params = array(
                $username,
                $password,
                $password_key,
                "0",
                $this->input->post('user_mail'),
                $this->input->post('petugas_name'),
                $this->input->post('petugas_jabatan'),
                $this->input->post('petugas_telepon'),
                $this->input->post('petugas_alamat'),
                $this->input->post('user_img'),
                $this->com_user['user_id'],
                $this->input->post('user_id')
            );

            // insert
            if ($this->m_operator_mini->edit($params)) {                
                    // upload foto
                    if (!empty($_FILES['user_img']['tmp_name'])) {
                        // load
                        $this->load->library('tupload');
                        // delete
                        $filepath = 'resource/doc/images/users/' . $this->com_user['user_img'];
                        if (is_file($filepath)) {
                            unlink($filepath);
                        }
                        // upload config
                        $config['upload_path'] = 'resource/doc/images/users/';
                        $config['allowed_types'] = 'gif|jpg|png';
                        $config['file_name'] = $this->com_user['user_id'];
                        $this->tupload->initialize($config);
                        // process upload images
                        if ($this->tupload->do_upload_image('user_img', false, 160)) {
                            $data = $this->tupload->data();
                            $this->m_operator_mini->update_user_img(array($data['file_name'], $this->com_user['user_id']));
                        } else {
                            // jika gagal
                            $this->tnotification->set_error_message($this->tupload->display_errors());
                        }
                    }

                    // notification
                    $this->tnotification->delete_last_field();
                    $this->tnotification->sent_notification("success", "Data berhasil disimpan");
                } else {
                    // default error
                    $this->tnotification->sent_notification("error", "Data gagal disimpan");
                }
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
            // default redirect
            redirect("pengaturan/profile/");
        }
    }
    